package com.huaxinkaiye.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.huaxinkaiye.R;
import com.huaxinkaiye.Zxing.CaptureActivity;
import com.huaxinkaiye.bean.ListBean;
import com.huaxinkaiye.utils.SimpleClient;
import com.huaxinkaiye.utils.ToastUtil;
import com.huaxinkaiye.utils.UrlUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChuJianFaildsActivity extends Activity implements OnClickListener {
	private ImageView im_back, im_chongxinsaoma;
	private LinearLayout lin_failds;
	public List<NameValuePair> params;
	public TextView te_chepaihao, te_chuangjiantime, te_danganbianhao;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			// case 1:
			// jiexiListData(msg.obj.toString());
			// break;
			case 2:
				JixiSousuoData(msg.obj.toString());
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chujianfailds);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		im_back = (ImageView) findViewById(R.id.im_back);
		im_back.setOnClickListener(this);
		lin_failds = (LinearLayout) findViewById(R.id.lin_failds);
		im_chongxinsaoma = (ImageView) findViewById(R.id.im_chongxinsaoma);
		im_chongxinsaoma.setOnClickListener(this);
		te_chepaihao = (TextView) findViewById(R.id.te_chepaihao);
		te_chuangjiantime = (TextView) findViewById(R.id.te_chuangjiantime);
		te_danganbianhao = (TextView) findViewById(R.id.te_danganbianhao);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_back:
			finish();
			break;
		case R.id.im_chongxinsaoma:
			Intent intent = new Intent(this, CaptureActivity.class);
			startActivityForResult(intent, 100);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		/**
		 * 拿到解析完成的字符串
		 */
		if (data != null) {
			String saoma = data.getStringExtra("result");
			searchD(saoma);
		}
	}

	protected void searchD(String sousuo) {
		// TODO Auto-generated method stub
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("pageNo", 1 + ""));
		params.add(new BasicNameValuePair("pageSize", 10 + ""));
		params.add(new BasicNameValuePair("sort", "id"));
		params.add(new BasicNameValuePair("direction", "desc"));
		params.add(new BasicNameValuePair("plateNumber", sousuo));
		params.add(new BasicNameValuePair("recordNo", sousuo));
		new Thread() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				String data;
				try {
					data = SimpleClient
							.doPost(UrlUtils.chujianlist_url, params);
					Log.e("search_chujianlist_data", data);
					Message mes = new Message();
					mes.what = 2;
					mes.obj = data;
					handler.sendMessage(mes);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
	}

	protected void JixiSousuoData(String s) {
		// TODO Auto-generated method stub
		JSONObject json;
		try {
			json = new JSONObject(s);
			// Log.e("s.substring(558, 585)", s.substring(500, 580));
			Gson gson = new Gson();
			ListBean listbean = gson.fromJson(s, ListBean.class);
			if (listbean.getRows().size() == 0) {
				ToastUtil.show(this, "没有搜到");
			} else {
				te_chepaihao.setText("车牌号： "
						+ listbean.getRows().get(0).getPlateNumber());
				te_chuangjiantime.setTag("创建时间： "
						+ listbean.getRows().get(0).getCreateTime());
				te_danganbianhao.setText("档案编号： "
						+ listbean.getRows().get(0).getBarCode());
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
